package com.baoleme.controller;

import com.baoleme.common.Result;

import com.baoleme.dto.UpdateDefaultAddressDTO;
import com.baoleme.entity.AddressBook;
import com.baoleme.service.AddressBookService;

import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuyunbin
 * @since 2024-09-23
 */
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Resource
    AddressBookService addressBookService;

    @Operation(summary = "新增地址")
    @PostMapping("addAddress")
    public Result<Boolean> addAddress(@RequestBody @Valid AddressBook addressBook){

        boolean save = addressBookService.save(addressBook);

        return Result.success(save);
    }




    @Operation(summary = "根据Id查询地址")
    @GetMapping("{id}")
    public  Result<AddressBook> queryAddressById(@PathVariable("id") long id){

        AddressBook address = addressBookService.getById(id);

        return Result.success(address);
    }


    @Operation(summary = "根据Id删除地址")
    @GetMapping("deleteAddress")
    public Result<Boolean> deleteAddress(@RequestBody String id){

        boolean flag = addressBookService.removeById(id);

        return Result.success(flag);
    }


    @Operation(summary = "根据Id修改地址")
    @PostMapping ("updateAddress")
    public Result<Boolean> updateAddress(@RequestBody @Valid AddressBook addressBook){

        boolean flag = addressBookService.updateById(addressBook);

        return Result.success(flag);
    }


    @Operation(summary = "查询当前用户的所有地址")
    @PostMapping("queryAllAddressByUserId")
    public Result<List<AddressBook>> queryAllAddressByUserId(String userId){

            List<AddressBook> addressBooks = addressBookService.queryAllAddressByUserId(userId);


            return Result.success(addressBooks);
    }

    @Operation(summary = "查询默认地址")
    @GetMapping("queryDefaultAddress")
    public Result<AddressBook> queryDefaultAddress(String userId){

        AddressBook addressBooks = addressBookService.querydefaultAddress(userId);


        return Result.success(addressBooks);
    }

    @Operation(summary = "设置默认地址")
    @PutMapping("updateDefaultAddress")
    public Result<Boolean> updateDefaultAddress(@RequestBody UpdateDefaultAddressDTO updateDefaultAddressDTO){


        boolean flag = addressBookService.updatedefaultAddress(updateDefaultAddressDTO);


        return Result.success(flag);
    }



}


